package com.xinghen.common.base.model.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @author xinghen
 * @ClassName: RestResult
 * @Package: com.xinghen.common.base.model.response
 * @Description rest 返回结果
 * @create 2018-04-13 10:40
 */
@Data
public class RestResult<T> implements Serializable {

    private static final long serialVersionUID = 4893280118017319089L;

    /**
     * 成功码.
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 成功信息.
     */
    public static final String SUCCESS_MESSAGE = "操作成功";

    /**
     * 错误码.
     */
    public static final int ERROR_CODE = 500;

    /**
     * 错误信息.
     */
    public static final String ERROR_MESSAGE = "内部异常";

    /**
     * 错误码：参数非法
     */
    public static final int ILLEGAL_ARGUMENT_CODE_ = 100;

    /**
     * 错误信息：参数非法
     */
    public static final String ILLEGAL_ARGUMENT_MESSAGE = "参数非法";

    /**
     * 返回码
     */
    private int code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回结果数据
     */
    private T result;

    public static <T> RestResult<T> ok(T data) {
        return new RestResult<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> RestResult<T> ok() {
        return ok(null);
    }

    public static <T> RestResult<T> error(String message) {
        return new RestResult<>(ERROR_CODE, message);
    }

    public static <T> RestResult<T> error() {
        return error(ERROR_MESSAGE);
    }

    public RestResult(int code, String message) {
        this(code, message, null);
    }

    public RestResult() {
    }

    public RestResult(int code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }
}
